package com.zycfc.zsf.boot.distributedlock;

import com.zycfc.zsf.boot.distributedlock.enums.*;
import com.zycfc.zsf.boot.util.text.*;
import org.slf4j.*;

public class DistributedLockUtil
{
    private static final Logger log;
    private static final String LCOK_INSTANCE = "\u5206\u5e03\u5f0f\u9501\u5b9e\u4f8b";
    private static final String LCOK_TYPE = "\u5206\u5e03\u5f0f\u9501\u7c7b\u578b";
    private static final String CONSTRUCT_LCOK_EXCEPTION = "DistributedLockUtil\u6784\u9020\u5206\u5e03\u5f0f\u9501\u5bf9\u8c61[group:{},lockName:{},expireTime:{},tryLockTime:{},distributedLockType:{}]\u53d1\u751f\u5f02\u5e38:";
    
    public static IDistributedLock getNewDistributedLock(final String group, final String lockName, final long expireTime) {
        return getNewDistributedLock(group, lockName, expireTime, DistributedLockTypeEnum.REDIS);
    }
    
    public static IDistributedLock getNewDistributedLock(final String group, final String lockName, final long expireTime, final long tryLockTime) {
        return getNewDistributedLock(group, lockName, expireTime, tryLockTime, DistributedLockTypeEnum.REDIS);
    }
    
    public static IDistributedLock getNewDistributedLock(final String group, final String lockName, final long expireTime, final DistributedLockTypeEnum distributedLockType) {
        return getNewDistributedLock(group, lockName, expireTime, 0L, distributedLockType);
    }
    
    public static IDistributedLock getNewDistributedLock(final String group, final String lockName, final long expireTime, final long tryLockTime, final DistributedLockTypeEnum distributedLockType) {
        IDistributedLock distributedLock = null;
        try {
            CheckUtil.notEmpty((Object)distributedLockType, "\u5206\u5e03\u5f0f\u9501\u7c7b\u578b");
            distributedLock = (DistributedLockTypeEnum.ZOOKEEPER.equals(distributedLockType) ? new DistributedZkLock(group, lockName) : ((tryLockTime > 0L) ? new DistributedRedisLock(group, lockName, expireTime, tryLockTime) : new DistributedRedisLock(group, lockName, expireTime)));
        }
        catch (Throwable e) {
            DistributedLockUtil.log.error("DistributedLockUtil\u6784\u9020\u5206\u5e03\u5f0f\u9501\u5bf9\u8c61[group:{},lockName:{},expireTime:{},tryLockTime:{},distributedLockType:{}]\u53d1\u751f\u5f02\u5e38:", new Object[] { group, lockName, expireTime, tryLockTime, distributedLockType.name(), e });
        }
        return distributedLock;
    }
    
    public static boolean tryLock(final IDistributedLock distributedLock) {
        CheckUtil.notNull((Object)distributedLock, "\u5206\u5e03\u5f0f\u9501\u5b9e\u4f8b");
        return distributedLock.tryLock();
    }
    
    public static boolean releaseLock(final IDistributedLock distributedLock) {
        CheckUtil.notNull((Object)distributedLock, "\u5206\u5e03\u5f0f\u9501\u5b9e\u4f8b");
        return distributedLock.releaseLock();
    }
    
    static {
        log = LoggerFactory.getLogger((Class)DistributedLockUtil.class);
    }
}
